package com.project.collectAPI.config;

import com.project.collectAPI.entity.Backupsupermarketoldtask;
import com.project.collectAPI.entity.Curtask;
import com.project.collectAPI.entity.Curtaskforsupermarket;
import com.project.collectAPI.entity.Savedata;
import com.project.collectAPI.service.BackupsupermarketoldtaskService;
import com.project.collectAPI.service.CurtaskService;
import com.project.collectAPI.service.CurtaskforsupermarketService;
import com.project.collectAPI.service.SavedataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/*
* 对以上代码注意点的说明：
@Component 注解不要漏掉。
使用@Autowired注入一个正常的channelMapper111，并声明一个工具类方法中要使用的channelMapper。
在@PostConstruct注解中，对channelMapper进行赋值。
最后在静态方法refreshChannalStatus中就可以使用channelMapper操作数据库啦。
* */

@Component
public class DataSourceConfig {

    @Resource
    private SavedataService savedataService;
    private static SavedataService savedataServiceOut;

    @Resource
    private CurtaskService curtaskService;
    private static CurtaskService curtaskServiceOut;

    @Resource
    private CurtaskforsupermarketService curtaskforsupermarketService;
    private static CurtaskforsupermarketService curtaskforsupermarketServiceOut;

    @Resource
    private BackupsupermarketoldtaskService backupsupermarketoldtaskService;
    private static BackupsupermarketoldtaskService backupsupermarketoldtaskServiceOut;

    @PostConstruct
    public void init() {
        savedataServiceOut = savedataService;
        curtaskServiceOut=curtaskService;
        curtaskforsupermarketServiceOut=curtaskforsupermarketService;
        backupsupermarketoldtaskServiceOut=backupsupermarketoldtaskService;
    }




    /**
     * 读取当前任务
     * */
    public static String getTaskHistory() throws Exception {
        try {
            String curtask=curtaskforsupermarketServiceOut.getTask();
            return curtask;
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("数据库暂无任务数据..............................");
        }
        return null;
    }




    /**
     * 记录当前任务索引
     *
     */
    public static void setTaskHistory(int taskId,int firstArrIndex,int secondArrIndex,String taskName,String secondarrays) throws Exception {
        try {
            curtaskforsupermarketServiceOut.setTask(taskId,firstArrIndex,secondArrIndex,taskName,secondarrays);
        } catch (Exception e) {
            System.out.println("设置任务失败..."+e.getMessage());
        }
    }





    /**
     * 查询当前店铺最后一条分类
     * @param storeName 当前店铺名称
     * @return 主类:分类
     * @return 错误返回 null
     * */
    public static String getNearClass(String storeName) {
       return savedataServiceOut.getNearClass(storeName);
    }



    /**
     * 查询数据库最近 主类+分类的 100条记录
     * @param TargetName 查找最新抓取的类目  格式: 主类:分类
     * */
    public static List<Savedata> getNearDatasList(String storeName, String TargetName, int Threshold){
        if(TargetName.indexOf(":")!=-1){
            String[] TempArr= TargetName.split(":");
            String main=TempArr[0];
            String sub=TempArr[1];
            System.out.println("最近主类:"+main+",最近分类:"+sub);
            List<Savedata> getNearDatasList=savedataServiceOut.getNearDatasList(storeName,main,sub,Threshold);
            return getNearDatasList;
        }else{
            System.out.println("类目不存在或格式有误");
        }
        return null;
    }

    /**
     * 添加任务数组
    * */
    public static int addCurTask(String[] storeNames){
        if(storeNames.length<=0) return -1;
        if(storeNames.length>0){
            //List<Curtask> taskList=new ArrayList<>();
            for (int i = 0; i <storeNames.length-1; i++) {
                Curtask curtask=new Curtask();
                curtask.setTaskindex(i);
                curtask.setTaskname(storeNames[i]);
                curtaskServiceOut.addTask(curtask);
            }
            //批量添加不能去重复
            /*if(taskList.size()>0){
                curtaskServiceOut.saveBatch(taskList);
            }*/
            return 0;
        }
        return -1;
    }


    /**
     * 设置任务状态
     * */
    public static int setCurTaskStatus(int arrIndex,String status){
        try {
            curtaskServiceOut.updateTaskStatus(arrIndex,status);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }


    /**
     * 读取当前任务
     * */
    public static Curtask getCurTask(){
        try {
            Curtask curtask=curtaskServiceOut.getCurTask();
            return curtask;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 获取剩余任务数量
     * */

    public static int getTaskNumber(){
        try {
            return curtaskServiceOut.getTaskNumber();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 删除所有任务
     * */
    public static int clearAllTasks(){
        try {
            curtaskServiceOut.deleteAllTask();
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }



    /**
     * 更新任务状态
     * */
    public static void UpdateStatusById(int taskId,String status) throws Exception {
        try {
            curtaskforsupermarketServiceOut.updateStatusById(taskId,status);
        } catch (Exception e) {
            System.out.println("DataSourceConfig.UpdateStatusById更新任务状态失败...");
        }
    }

    /**
     * 获取任务状态
     * */
    public static String GetStatusById(int taskId){
        try {
            return curtaskforsupermarketServiceOut.getStatusById(taskId);
        } catch (Exception e) {
            System.out.println("DataSourceConfig.GetStatusById获取状态失败...");
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 更新二级数组字符串
     * */
    public static void UpdateSecondArrsById(int taskId,String secondarrays) throws Exception {
        try {
            curtaskforsupermarketServiceOut.updateSecondArrsById(taskId,secondarrays);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("DataSourceConfig.UpdateSecondArrsById更新任务状态失败...");
        }
    }



    /**
     * 获取二级数组
     * */
    public static String GetSecondArrsById(int taskId)  {
          return  curtaskforsupermarketServiceOut.getSecondArrs(taskId);
    }

    /**
     * 备份旧任务
     * */
    public static int backupOldTask(int taskId, Backupsupermarketoldtask backupsupermarketoldtask){
        try {
            backupsupermarketoldtaskServiceOut.updateDataFile(taskId,backupsupermarketoldtask);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }


    /**
     * 设置任务内容
     * */
    public static int setCurTask(int taskId, Curtaskforsupermarket curtaskforsupermarket){
        try {
            curtaskforsupermarketServiceOut.updateDataFile(taskId,curtaskforsupermarket);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

} //Class End
